# Utils.py
# Copyright (C) 2010 Daniel Callander
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License

def underlineCode():
	"""Returns the underline control code."""
	return '\x1F'

def boldCode():
	"""Returns the bold control code."""
	return '\x02'

def colorCode(**colors):
	"""Returns the colour control code.
	
	colours:
		fg - foreground
		bg - background"""
	
	# Check that the arguments were actually passed.
	if colors.has_key('fg'):
		fg = colors['fg']
	else:
		fg = None
	if colors.has_key('bg'):
		bg = colors['bg']
	else:
		bg = None
	
	code = "\x03"
	
	# If we have a foreground, hopefully, add that.
	if not fg == None:
		code = code + str(fg).rjust(2,'0')
	
	# If no foreground is specifified, assume it to want 01 (black).
	# otherwise assume one was infact specified.
	if not bg == None and fg == None:
		code = code + '01,' + str(bg).rjust(2,'0')
	elif bg:
		code = code + ',' + str(bg).rjust(2,'0')
	
	return code

def normalCode():
	"""Returns the normal control code."""
	return '\x0F'

def stripCodes(message):
	"""Strips all control codes from a message."""
	message = message.replace(underlineCode(),'')
	message = message.replace(boldCode(),'')
	message = message.replace(colorCode(),'')
	message = message.replace(normalCode(),'')
	
	return message

# ------------------------------------------------------------------------------

def stripNickStatus(nick):
	"""Strips all user status symbols from a nickname"""
	nick = nick.replace("+", "")
	nick = nick.replace("@", "")
	nick = nick.replace("%", "")
	nick = nick.replace("~", "")
	
	return nick